﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace WhatIf
{
    public partial class RiskMatrixPage : UserControl
    {   
       // private string [,] _rmMatrix = new string[5,5];
        public RiskMatrixPage()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            cbxRM_00.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[0, 0]);
            cbxRM_01.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[0, 1]);
            cbxRM_02.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[0, 2]);
            cbxRM_03.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[0, 3]);
            cbxRM_04.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[0, 4]);
            cbxRM_10.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[1, 0]);
            cbxRM_11.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[1, 1]);
            cbxRM_12.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[1, 2]);
            cbxRM_13.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[1, 3]);
            cbxRM_14.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[1, 4]);
            cbxRM_20.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[2, 0]);
            cbxRM_21.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[2, 1]);
            cbxRM_22.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[2, 2]);
            cbxRM_23.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[2, 3]);
            cbxRM_24.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[2, 4]);
            cbxRM_30.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[3, 0]);
            cbxRM_31.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[3, 1]);
            cbxRM_32.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[3, 2]);
            cbxRM_33.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[3, 3]);
            cbxRM_34.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[3, 4]);
            cbxRM_40.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[4, 0]);
            cbxRM_41.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[4, 1]);
            cbxRM_42.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[4, 2]);
            cbxRM_43.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[4, 3]);
            cbxRM_44.SelectedIndex = GetSelectedIndex(Program.Workspace.GetRiskMatrix()[4, 4]);

            tbxRM_R1.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmR1;
            tbxRM_R2.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmR2;
            tbxRM_R3.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmR3;
            tbxRM_R4.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmR4;
            tbxRM_R5.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmR5;
            tbxRM_C1.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmC1;
            tbxRM_C2.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmC2;
            tbxRM_C3.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmC3;
            tbxRM_C4.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmC4;
            tbxRM_C5.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmC5;
            tbx_RMD_R11.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR11;
            tbx_RMD_R12.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR12;
            tbx_RMD_R13.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR13;
            tbx_RMD_R21.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR21;
            tbx_RMD_R22.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR22;
            tbx_RMD_R23.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR23;
            tbx_RMD_R31.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR31;
            tbx_RMD_R32.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR32;
            tbx_RMD_R33.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR33;
            tbx_RMD_R41.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR41;
            tbx_RMD_R42.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR42;
            tbx_RMD_R43.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR43;
            tbx_RMD_R51.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR51;
            tbx_RMD_R52.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR52;
            tbx_RMD_R53.Text = Program.Workspace.WhatIfProject.RiskMatrix.RmdR53;
        }

        private int GetSelectedIndex(string s){
            switch (s){
                case "A":
                    return 0;
                case "B":
                    return 1;
                case "C":
                    return 2;
                case "D":
                    return 3;
                case "E":
                    return 4;
                default:break;
            }
            return -1;
        }

        private void btnEdit_Click(object sender, EventArgs e){
            if(tbxRM_R1.Enabled){
                return;
            }
            btnDefault.Enabled = true;
            if (MessageBox.Show("是否确认修改风险矩阵?修改此风险矩阵请参考公司相关规定,此次修改只对本机用户生效", "Confirm Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                tbxRM_R1.Enabled = true;
                tbxRM_R2.Enabled = true;
                tbxRM_R3.Enabled = true;
                tbxRM_R4.Enabled = true;
                tbxRM_R5.Enabled = true;
                tbxRM_C1.Enabled = true;
                tbxRM_C2.Enabled = true;
                tbxRM_C3.Enabled = true;
                tbxRM_C4.Enabled = true;
                tbxRM_C5.Enabled = true;
                tbx_RMD_R11.Enabled = true;
                tbx_RMD_R12.Enabled = true;
                tbx_RMD_R13.Enabled = true;
                tbx_RMD_R21.Enabled = true;
                tbx_RMD_R22.Enabled = true;
                tbx_RMD_R23.Enabled = true;
                tbx_RMD_R31.Enabled = true;
                tbx_RMD_R32.Enabled = true;
                tbx_RMD_R33.Enabled = true;
                tbx_RMD_R41.Enabled = true;
                tbx_RMD_R42.Enabled = true;
                tbx_RMD_R43.Enabled = true;
                tbx_RMD_R51.Enabled = true;
                tbx_RMD_R52.Enabled = true;
                tbx_RMD_R53.Enabled = true;
                cbxRM_40.Enabled = true;
                cbxRM_41.Enabled = true;
                cbxRM_42.Enabled = true;
                cbxRM_43.Enabled = true;
                cbxRM_44.Enabled = true;
                cbxRM_30.Enabled = true;
                cbxRM_31.Enabled = true;
                cbxRM_32.Enabled = true;
                cbxRM_33.Enabled = true;
                cbxRM_34.Enabled = true;
                cbxRM_20.Enabled = true;
                cbxRM_21.Enabled = true;
                cbxRM_22.Enabled = true;
                cbxRM_23.Enabled = true;
                cbxRM_24.Enabled = true;
                cbxRM_10.Enabled = true;
                cbxRM_11.Enabled = true;
                cbxRM_12.Enabled = true;
                cbxRM_13.Enabled = true;
                cbxRM_14.Enabled = true;
                cbxRM_00.Enabled = true;
                cbxRM_01.Enabled = true;
                cbxRM_02.Enabled = true;
                cbxRM_03.Enabled = true;
                cbxRM_04.Enabled = true;
            }

            
        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            if (!tbxRM_R1.Enabled)
            {
                return;
            }
            btnDefault.Enabled = false;

            Program.Workspace.GetRiskMatrix()[0, 0] = cbxRM_00.Text;
            Program.Workspace.GetRiskMatrix()[0, 1] = cbxRM_01.Text;
            Program.Workspace.GetRiskMatrix()[0, 2] = cbxRM_02.Text;
            Program.Workspace.GetRiskMatrix()[0, 3] = cbxRM_03.Text;
            Program.Workspace.GetRiskMatrix()[0, 4] = cbxRM_04.Text;
            Program.Workspace.GetRiskMatrix()[1, 0] = cbxRM_10.Text;
            Program.Workspace.GetRiskMatrix()[1, 1] = cbxRM_11.Text;
            Program.Workspace.GetRiskMatrix()[1, 2] = cbxRM_12.Text;
            Program.Workspace.GetRiskMatrix()[1, 3] = cbxRM_13.Text;
            Program.Workspace.GetRiskMatrix()[1, 4] = cbxRM_14.Text;
            Program.Workspace.GetRiskMatrix()[2, 0] = cbxRM_20.Text;
            Program.Workspace.GetRiskMatrix()[2, 1] = cbxRM_21.Text;
            Program.Workspace.GetRiskMatrix()[2, 2] = cbxRM_22.Text;
            Program.Workspace.GetRiskMatrix()[2, 3] = cbxRM_23.Text;
            Program.Workspace.GetRiskMatrix()[2, 4] = cbxRM_24.Text;
            Program.Workspace.GetRiskMatrix()[3, 0] = cbxRM_30.Text;
            Program.Workspace.GetRiskMatrix()[3, 1] = cbxRM_31.Text;
            Program.Workspace.GetRiskMatrix()[3, 2] = cbxRM_32.Text;
            Program.Workspace.GetRiskMatrix()[3, 3] = cbxRM_33.Text;
            Program.Workspace.GetRiskMatrix()[3, 4] = cbxRM_34.Text;
            Program.Workspace.GetRiskMatrix()[4, 0] = cbxRM_40.Text;
            Program.Workspace.GetRiskMatrix()[4, 1] = cbxRM_41.Text;
            Program.Workspace.GetRiskMatrix()[4, 2] = cbxRM_42.Text;
            Program.Workspace.GetRiskMatrix()[4, 3] = cbxRM_43.Text;
            Program.Workspace.GetRiskMatrix()[4, 4] = cbxRM_44.Text;


            Program.Workspace.WhatIfProject.RiskMatrix.RmR1 = tbxRM_R1.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR2 = tbxRM_R2.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR3 = tbxRM_R3.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR4 = tbxRM_R4.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR5 = tbxRM_R5.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC1 = tbxRM_C1.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC2 = tbxRM_C2.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC3 = tbxRM_C3.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC4 = tbxRM_C4.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC5 = tbxRM_C5.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR11 = tbx_RMD_R11.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR12 = tbx_RMD_R12.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR13 = tbx_RMD_R13.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR21 = tbx_RMD_R21.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR22 = tbx_RMD_R22.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR23 = tbx_RMD_R23.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR31 = tbx_RMD_R31.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR32 = tbx_RMD_R32.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR33 = tbx_RMD_R33.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR41 = tbx_RMD_R41.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR42 = tbx_RMD_R42.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR43 = tbx_RMD_R43.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR51 = tbx_RMD_R51.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR52 = tbx_RMD_R52.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR53 = tbx_RMD_R53.Text;


            tbxRM_R1.Enabled = false;
            tbxRM_R2.Enabled = false;
            tbxRM_R3.Enabled = false;
            tbxRM_R4.Enabled = false;
            tbxRM_R5.Enabled = false;
            tbxRM_C1.Enabled = false;
            tbxRM_C2.Enabled = false;
            tbxRM_C3.Enabled = false;
            tbxRM_C4.Enabled = false;
            tbxRM_C5.Enabled = false;
            tbx_RMD_R11.Enabled = false;
            tbx_RMD_R12.Enabled = false;
            tbx_RMD_R13.Enabled = false;
            tbx_RMD_R21.Enabled = false;
            tbx_RMD_R22.Enabled = false;
            tbx_RMD_R23.Enabled = false;
            tbx_RMD_R31.Enabled = false;
            tbx_RMD_R32.Enabled = false;
            tbx_RMD_R33.Enabled = false;
            tbx_RMD_R41.Enabled = false;
            tbx_RMD_R42.Enabled = false;
            tbx_RMD_R43.Enabled = false;
            tbx_RMD_R51.Enabled = false;
            tbx_RMD_R52.Enabled = false;
            tbx_RMD_R53.Enabled = false;
            cbxRM_40.Enabled = false;
            cbxRM_41.Enabled = false;
            cbxRM_42.Enabled = false;
            cbxRM_43.Enabled = false;
            cbxRM_44.Enabled = false;
            cbxRM_30.Enabled = false;
            cbxRM_31.Enabled = false;
            cbxRM_32.Enabled = false;
            cbxRM_33.Enabled = false;
            cbxRM_34.Enabled = false;
            cbxRM_20.Enabled = false;
            cbxRM_21.Enabled = false;
            cbxRM_22.Enabled = false;
            cbxRM_23.Enabled = false;
            cbxRM_24.Enabled = false;
            cbxRM_10.Enabled = false;
            cbxRM_11.Enabled = false;
            cbxRM_12.Enabled = false;
            cbxRM_13.Enabled = false;
            cbxRM_14.Enabled = false;
            cbxRM_00.Enabled = false;
            cbxRM_01.Enabled = false;
            cbxRM_02.Enabled = false;
            cbxRM_03.Enabled = false;
            cbxRM_04.Enabled = false;
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            MessageBox.Show("风险矩阵保存成功!");
        }

        public void SaveWithoutRemider(){
            if (!tbxRM_R1.Enabled)
            {
                return;
            }

            btnDefault.Enabled = false;
            Program.Workspace.GetRiskMatrix()[0, 0] = cbxRM_00.Text;
            Program.Workspace.GetRiskMatrix()[0, 1] = cbxRM_01.Text;
            Program.Workspace.GetRiskMatrix()[0, 2] = cbxRM_02.Text;
            Program.Workspace.GetRiskMatrix()[0, 3] = cbxRM_03.Text;
            Program.Workspace.GetRiskMatrix()[0, 4] = cbxRM_04.Text;
            Program.Workspace.GetRiskMatrix()[1, 0] = cbxRM_10.Text;
            Program.Workspace.GetRiskMatrix()[1, 1] = cbxRM_11.Text;
            Program.Workspace.GetRiskMatrix()[1, 2] = cbxRM_12.Text;
            Program.Workspace.GetRiskMatrix()[1, 3] = cbxRM_13.Text;
            Program.Workspace.GetRiskMatrix()[1, 4] = cbxRM_14.Text;
            Program.Workspace.GetRiskMatrix()[2, 0] = cbxRM_20.Text;
            Program.Workspace.GetRiskMatrix()[2, 1] = cbxRM_21.Text;
            Program.Workspace.GetRiskMatrix()[2, 2] = cbxRM_22.Text;
            Program.Workspace.GetRiskMatrix()[2, 3] = cbxRM_23.Text;
            Program.Workspace.GetRiskMatrix()[2, 4] = cbxRM_24.Text;
            Program.Workspace.GetRiskMatrix()[3, 0] = cbxRM_30.Text;
            Program.Workspace.GetRiskMatrix()[3, 1] = cbxRM_31.Text;
            Program.Workspace.GetRiskMatrix()[3, 2] = cbxRM_32.Text;
            Program.Workspace.GetRiskMatrix()[3, 3] = cbxRM_33.Text;
            Program.Workspace.GetRiskMatrix()[3, 4] = cbxRM_34.Text;
            Program.Workspace.GetRiskMatrix()[4, 0] = cbxRM_40.Text;
            Program.Workspace.GetRiskMatrix()[4, 1] = cbxRM_41.Text;
            Program.Workspace.GetRiskMatrix()[4, 2] = cbxRM_42.Text;
            Program.Workspace.GetRiskMatrix()[4, 3] = cbxRM_43.Text;
            Program.Workspace.GetRiskMatrix()[4, 4] = cbxRM_44.Text;


            Program.Workspace.WhatIfProject.RiskMatrix.RmR1 = tbxRM_R1.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR2 = tbxRM_R2.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR3 = tbxRM_R3.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR4 = tbxRM_R4.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmR5 = tbxRM_R5.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC1 = tbxRM_C1.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC2 = tbxRM_C2.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC3 = tbxRM_C3.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC4 = tbxRM_C4.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmC5 = tbxRM_C5.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR11 = tbx_RMD_R11.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR12 = tbx_RMD_R12.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR13 = tbx_RMD_R13.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR21 = tbx_RMD_R21.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR22 = tbx_RMD_R22.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR23 = tbx_RMD_R23.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR31 = tbx_RMD_R31.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR32 = tbx_RMD_R32.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR33 = tbx_RMD_R33.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR41 = tbx_RMD_R41.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR42 = tbx_RMD_R42.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR43 = tbx_RMD_R43.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR51 = tbx_RMD_R51.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR52 = tbx_RMD_R52.Text;
            Program.Workspace.WhatIfProject.RiskMatrix.RmdR53 = tbx_RMD_R53.Text;


            tbxRM_R1.Enabled = false;
            tbxRM_R2.Enabled = false;
            tbxRM_R3.Enabled = false;
            tbxRM_R4.Enabled = false;
            tbxRM_R5.Enabled = false;
            tbxRM_C1.Enabled = false;
            tbxRM_C2.Enabled = false;
            tbxRM_C3.Enabled = false;
            tbxRM_C4.Enabled = false;
            tbxRM_C5.Enabled = false;
            tbx_RMD_R11.Enabled = false;
            tbx_RMD_R12.Enabled = false;
            tbx_RMD_R13.Enabled = false;
            tbx_RMD_R21.Enabled = false;
            tbx_RMD_R22.Enabled = false;
            tbx_RMD_R23.Enabled = false;
            tbx_RMD_R31.Enabled = false;
            tbx_RMD_R32.Enabled = false;
            tbx_RMD_R33.Enabled = false;
            tbx_RMD_R41.Enabled = false;
            tbx_RMD_R42.Enabled = false;
            tbx_RMD_R43.Enabled = false;
            tbx_RMD_R51.Enabled = false;
            tbx_RMD_R52.Enabled = false;
            tbx_RMD_R53.Enabled = false;
            cbxRM_40.Enabled = false;
            cbxRM_41.Enabled = false;
            cbxRM_42.Enabled = false;
            cbxRM_43.Enabled = false;
            cbxRM_44.Enabled = false;
            cbxRM_30.Enabled = false;
            cbxRM_31.Enabled = false;
            cbxRM_32.Enabled = false;
            cbxRM_33.Enabled = false;
            cbxRM_34.Enabled = false;
            cbxRM_20.Enabled = false;
            cbxRM_21.Enabled = false;
            cbxRM_22.Enabled = false;
            cbxRM_23.Enabled = false;
            cbxRM_24.Enabled = false;
            cbxRM_10.Enabled = false;
            cbxRM_11.Enabled = false;
            cbxRM_12.Enabled = false;
            cbxRM_13.Enabled = false;
            cbxRM_14.Enabled = false;
            cbxRM_00.Enabled = false;
            cbxRM_01.Enabled = false;
            cbxRM_02.Enabled = false;
            cbxRM_03.Enabled = false;
            cbxRM_04.Enabled = false;
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            if(new WarringForm("是否要恢复风险矩阵设置？").ShowDialog() == DialogResult.OK){
                Program.Workspace.WhatIfProject.RiskMatrix = new RiskMatrix();
                LoadData();
            }
        }
    }
}
